//
//  ApproveAgreeViewController.m
//  CloudMD
//
//  Created by centling on 16/3/25.
//  Copyright © 2016年 HuaZhengInfo. All rights reserved.
//

#import "ApproveAgreeViewController.h"

@interface ApproveAgreeViewController ()<UIAlertViewDelegate> {
    UIView * _mainView;
    XYTextView * _textView; //审核意见
    NSString * _state; // 状态
    NSString * _ID; // ID
}

@end

@implementation ApproveAgreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"同意审核";
    UIButton * leftButton = [GDFactory creatUIButtonWithFrame:CGRectMake(0, 0, 10, 15) backImage:@"left_arrow" title:nil titleLableColcor:nil titleLabelFont:nil];
    [leftButton addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self buildMainUI];
}

#pragma mark -- 创建UI
- (void)buildMainUI{
    
    _mainView = [GDFactory creatUIViewWithFrame:CGRectMake(0, 0, SCR_W, SCR_H) backColor:RGBA(235, 235, 235, 1.0)];
    [self.view addSubview:_mainView];
    
    UILabel * label3 = [GDFactory creatUILabelWithFrame:CGRectMake(10, 25, 100, 30) text:@"审核意见:" textColor:RED textFont:[UIFont systemFontOfSize:16]];

    [_mainView addSubview:label3];
    
    
    // 问题描述
    
    _textView = [[XYTextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(label3.frame)+5, SCR_W - 20, SCR_H/8)];
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.font = [UIFont systemFontOfSize:16.0];
    _textView.textColor = [UIColor blackColor];
    _textView.textAlignment = NSTextAlignmentLeft;
    //自定义键盘上面完成toolbar
    UIToolbar *customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCR_W,40}];
    customAccessoryView.barTintColor = [UIColor whiteColor];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame = CGRectMake(0, 0, 40, 40);
    [sendButton setTitle:@"完成" forState:UIControlStateNormal];
    [sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *finish = [[UIBarButtonItem alloc] initWithCustomView:sendButton];
    [customAccessoryView setItems:@[space,space,finish]];
    _textView.inputAccessoryView = customAccessoryView;
    
    [_mainView addSubview:_textView];
    
    
    //  同意按钮
    UIButton * grabBtn = [[UIButton alloc] init];
    if (iPhone4) {
        grabBtn.frame = CGRectMake(0, 0, SCR_W/2, 30);
        grabBtn.center = CGPointMake(SCR_W * 0.5, CGRectGetMaxY(_textView.frame)+ 50);
    }else{
        
        grabBtn.frame = CGRectMake(0, 0, SCR_W/2, 40);
        grabBtn.center = CGPointMake(SCR_W * 0.5, CGRectGetMaxY(_textView.frame)+ 60);
    }
    
    [grabBtn setBackgroundImage:[UIImage imageNamed:@"3在线下单"] forState:UIControlStateNormal];
    [grabBtn setTitle:@"同意审核" forState:UIControlStateNormal];
    [grabBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    grabBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [grabBtn addTarget:self action:@selector(agreeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [_mainView addSubview:grabBtn];
    
}

// 键盘完成按钮
- (void)doneClick {
    [_textView resignFirstResponder];
}

- (void)agreeBtnClick {
    NSString * remark = _textView.text;
    if ([remark isEqualToString:@""]) {
        [MBProgressHUD HUDShowMessage:@"请填写审核意见" addedToView:self.view];
        return;
    }
    [self alertError:@"确认同意审核？"];
}

#pragma mark - 弹框提示
- (void)alertError:(NSString *)error{
    
    // 1.弹框
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:error delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        [self agreeAction];
    }
}

- (void)agreeAction {
    NSArray * keyArray = @[@"GUID",@"USERID",@"USERCODE",@"ISAGREE",@"APPROVEID",@"CONTENT",@"FHGWCODE",@"THAPPROVEID"];
    NSArray * valueArray = @[_GUID,USER_ID,USER_CODE,@1,@(self.approveID),_textView.text,@"",@""];
    NSDictionary * dict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,DeclarationApproveSubmit_URL];
    HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
    [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
        NSMutableArray * array = [http.returnArray mutableCopy];
        for (NSDictionary * dataDict in array) {
            _state = [dataDict objectForKey:@"STATUS"];
            _ID = [dataDict objectForKey:@"ID"];
        }
        
        if ([_state isEqualToString:@"SUCCESS"]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [MBProgressHUD HUDShowMessage:@"审核失败" addedToView:self.view];
        }
    } withTag:101];
}


- (void)clickLeft {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
